<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="商品spu" prop="spu">
                    <el-input v-model.trim="queryParams.spu" placeholder="请输入商品spu" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="合同批次号" prop="batch_id">
                    <el-input v-model.trim="queryParams.batch_id" placeholder="请输入合同批次号" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="供应商" prop="supply_id">
                    <SelectPage
                        v-model="queryParams.supply_id"
                        placeholder="请选择供应商"
                        :url="$api.supply.getSupplyList"
                        show_field="supply_name"
                        query_field="supply_name"
                        query_field2="spu"
                        filterable
                        clearable
                        popper-class="custom-select"
                        style="width: 240px"
                    ></SelectPage>
                </el-form-item>
                <el-form-item label="操作人" prop="operater_id">
                    <SelectPage
                        v-model="queryParams.operater_id"
                        placeholder="请选择操作人员"
                        :url="$api.purchase.getPurchaserList"
                        show_field="username"
                        show_id="operater_id"
                        query_field="keyword"
                        filterable
                        clearable
                        popper-class="custom-select"
                        style="width: 240px"
                    ></SelectPage>
                </el-form-item>
                <el-form-item label="审核状态" prop="confirm_status">
                    <el-select v-model="queryParams.confirm_status" placeholder="请选择审核状态" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in dict.confirmStatus" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="审核通过时间" prop="check_time">
                    <el-date-picker v-model="queryParams.check_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" @click="AddBatchNumber({}, 'add')">添加批次号</el-button>
                </el-col>      
                 <!-- <el-col :span="1.5">
                    <upload-button :action="action" @complete="getList"></upload-button>
                </el-col> -->
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

                <el-table v-loading="loading" :data="tableData" border script>
                    <el-table-column label="合同批次号" prop="batch_id" min-width="160" show-overflow-tooltip></el-table-column>
                    <el-table-column label="spu" prop="spu" min-width="90" show-overflow-tooltip></el-table-column>
                    <el-table-column label="图片" prop="image_url" min-width="90">
                        <template slot-scope="scope">
                            <ImageUploadImg v-model="scope.row.image_url" :readOnly="true" class="small-img"></ImageUploadImg>
                        </template>
                    </el-table-column>
                    <el-table-column label="供应商" prop="supply_name" min-width="200" show-overflow-tooltip></el-table-column>
                    <el-table-column label="审核状态" min-width="120" prop="confirm_status">
                        <template slot-scope="scope">
                            {{ scope.row.confirm_status | formatLabelByValueFuzzy(dict.confirmStatus) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作人" prop="create_operator" min-width="120" show-overflow-tooltip></el-table-column>
                    <el-table-column label="创建时间" min-width="160" prop="create_time"></el-table-column>
                    <el-table-column label="审核通过时间" min-width="160" prop="check_time"></el-table-column>
                    <el-table-column label="操作" min-width="120" fixed="right">
                        <template slot-scope="scope">
                            <!-- <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row, 'edit')">编辑</el-button>
                            <el-button type="text" @click="deleteItem(scope.row, scope.$index)" >删除</el-button> -->
                            <el-button type="text" @click="showContractDetailDialog(scope.row)">查看合同</el-button>
                            <el-button type="text" :disabled="scope.row.confirm_status!=2" @click="handleExport(scope.row)">在途导出</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>

        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" @success="handleQuery"></detail-dialog>
        <detail-model-dialog v-model="detailModelDialog.visible" v-if="detailModelDialog.visible" :params="detailModelDialog.data" @success="handleQuery"></detail-model-dialog>
    </div>
</template>

<script>
import { queryContractList, export_onway_sku, queryContractDel,queryContractAdd } from '@/api/product'
import { parseTime } from '@/utils/quickadmin'
import { STATUS_LIST } from '../constant'
import DetailDialog from '../components/ContractDetailDialog.vue'
import DetailModelDialog from '../components/ContractDetailModelDialog.vue'
import { exportFile } from '@/utils/index'
export default {
    name: 'AdvaccountList',
    components: { DetailDialog, DetailModelDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 店铺列表下拉
            shopList: [],
            salesmanList: [],
            platformList: [],
            STATUS_LIST,
            dict: {
                confirmStatus: [
                    { label: '待审核', value: 1 },
                    { label: '审核通过', value: 2 },
                ],
            },
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                spu: undefined,
                batch_id: undefined,
                supply_id: undefined,
                operater_id: undefined,
                confirm_status: undefined,
                create_time: [],
                check_time: [],
            },
            detailDialog: {
                visible: false,
                data: {},
            },
            detailModelDialog: {
                visible: false,
                data: {},
            },
            action: process.env.VUE_APP_BASE_API + '/product/Contract/import',
        }
    },
    created() {
        this.getList()
    },
    methods: {
        handleExport(row) {
            export_onway_sku({batch_id: row.batch_id})
                .then(res => {
                    console.log(res)
                    exportFile(res)
                })
                .catch(err => {
                    console.log(err)
                })
        },
        deleteItem(item, index){
            this.$confirm(`是否确认删除?`, '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                queryContractDel(item).then(res=>{
                    this.$message.success(res.msg)
                    this.model.tableData.splice(index, 1)
                    this.$emit('success')
                }).finally(()=>{
                    this.pageInfo.page = 1
                    this.getList()
                })
            }).catch(() => { })
        },
        AddBatchNumber(item, index){
            queryContractAdd(item).then(res=>{
                this.$message.success(res.msg)
                this.model.tableData.splice(index, 1)
                this.$emit('success')
                }).finally(()=>{
                    this.pageInfo.page = 1
                    this.getList()
                })
        },
        showDetailDialog(row, type) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = type
        },
        showContractDetailDialog(row) {
            this.detailModelDialog.visible = true
            this.detailModelDialog.data = row
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            if (qyparams.check_time?.length == 2) {
                qyparams.check_time = [parseTime(qyparams.check_time[0], 'y-m'), parseTime(qyparams.check_time[1], 'y-m')]
            }
            queryContractList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
<style scoped>
::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
</style>
